package com.caishi.lkx.common.yidun;

import com.netease.yidun.sdk.mobileverify.oneclicklogin.v1.MobileNumberGetRequest;
import com.netease.yidun.sdk.mobileverify.oneclicklogin.v1.MobileNumberGetResponse;
import com.netease.yidun.sdk.mobileverify.oneclicklogin.v1.MobileNumberResult;
import com.netease.yidun.sdk.mobileverify.oneclicklogin.v1.OneClickLoginClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lkx
 * @date 2024/6/13
 * @description
 */
@Slf4j
@Service
public class WangyiYidunPlugins {

    private OneClickLoginClient client;

    @Autowired
    private WangyiYidunConfig wangyiYidunConfig;

    protected  OneClickLoginClient createClient() {
        if (client == null){
            client = new OneClickLoginClient(wangyiYidunConfig.getAccessKeyId(), wangyiYidunConfig.getAccessKeySecret());
        }
        return client;
    }

    /**
     * 易盾一键登录获取手机号码
     * @param businessId 业务id
     * @param token 【不可重复使用】易盾token(有效期2分钟)
     * @param accessToken 【不可重复使用】运营商授权码(有效期2分钟)
     * @return
     */
    public MobileNumberResult getMobileNumber( String businessId, String token, String accessToken) {
        MobileNumberGetResponse response =  this.createClient().getMobileNumber(new MobileNumberGetRequest(businessId)
                .token(token)
                .accessToken(accessToken));
        if (response.getCode() == 200) {
            return response.getData();
        } else {
            log.error("易盾一键登录获取手机号失败，code:{},msg:{}", response.getCode(), response.getMsg());
            return null;
        }
    }

}
